package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.web.common.R;
import com.qf.web.pojo.Student;
import com.qf.web.service.StudentService;
import com.qf.web.service.impl.StudentServiceImpl;
import com.qf.web.utils.DateUtil;
import sun.rmi.runtime.NewThreadAction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;

@WebServlet("/updateStudent")
public class UpdateStudentServvlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doHead(req, resp);
    }

    @Override
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");

        String id = req.getParameter("id");

        String sex = req.getParameter("sex");

        String birthday = req.getParameter("birthday");

        String phone = req.getParameter("phone");

        String address = req.getParameter("address");

        String hobby = req.getParameter("hobby");

        Student student = new Student();
        student.setName(name);

        student.setId(Integer.parseInt(id));

        Date date = DateUtil.strToDate(birthday, "yyyy-MM-dd");
        student.setBirthday(date);

        student.setPhone(phone);

        student.setAddress(address);

        student.setHobby(hobby);

        student.setSex(sex);

        StudentService studentService = new StudentServiceImpl();

        int result = studentService.updateStudent(student);
        if (result == 1) {
        //修改成功
            //把java 对象转为JSON串
            String s = JSON.toJSONString(R.ok());
            resp.getWriter().println(s);
        } else {
            //修改失败
            String s = JSON.toJSONString(R.error());
            resp.getWriter().println(s);
        }

    }
    }

